package com.mdx.zhizhao.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdx.zhizhao.domain.Evaluate;
import com.mdx.zhizhao.domain.Job;
import com.mdx.zhizhao.persistence.EvaluateMapper;
import com.mdx.zhizhao.persistence.JobMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private JobMapper jobMapper;
    public int insertEvaluate(Evaluate evaluate) {
        return evaluateMapper.insert(evaluate);
    }
    public List<Evaluate> selectBySeeker(String seekerid) {
        QueryWrapper<Evaluate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seekerid", seekerid);
        List<Evaluate> evaluateList = evaluateMapper.selectList(queryWrapper);
        // 存储最终结果的列表
        List<Evaluate> resultList = new ArrayList<>();
        // 存储已经处理过的 jobname
        List<String> processedJobNames = new ArrayList<>();
        for (Evaluate evaluate : evaluateList){
            Job job = jobMapper.selectById(evaluate.getJobid());
            evaluate.setJobname(job.getJobname());
            // 将分数转换为 Double 类型进行平均值计算
            double averageScore = calculateAverageScore(evaluate);
            evaluate.setAveragescore(averageScore);
            String jobName = evaluate.getJobname();
            if (!processedJobNames.contains(jobName)) {
                // 如果该 jobname 未处理过，直接添加到结果列表
                resultList.add(evaluate);
                processedJobNames.add(jobName);
            } else {
                // 如果该 jobname 已经处理过，找出结果列表中相同 jobname 的元素
                for (Evaluate existingEvaluate : resultList) {
                    if (existingEvaluate.getJobname().equals(jobName)) {
                        if (evaluate.getAveragescore() > existingEvaluate.getAveragescore()) {
                            resultList.remove(existingEvaluate);
                            resultList.add(evaluate);
                        }
                        break;
                    }
                }
            }
        }
        resultList.sort(Comparator.comparingDouble(Evaluate::getAveragescore).reversed());


        return resultList;
    }
    private double calculateAverageScore(Evaluate evaluate) {
        // 将字符串类型的分数转换为 Double 类型并计算平均值
        double salaryScore = Double.parseDouble(evaluate.getSalaryscore());
        double locationScore = Double.parseDouble(evaluate.getLocationscore());
        double abilityScore = Double.parseDouble(evaluate.getAbilityscore());
        return (salaryScore + locationScore + abilityScore) / 3;
    }
      public Evaluate selectByEnterpriseIdAndJobId(String enterpriseId, String jobId, String seekerid) {
        QueryWrapper<Evaluate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterpriseid", enterpriseId);
        queryWrapper.eq("jobid", jobId);
        queryWrapper.eq("seekerid", seekerid);
        return evaluateMapper.selectOne(queryWrapper);
    }
}
